{extend name="main/index" /}
{block name="main"}

<!-- JQ ajax -->
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>  
        <script>  
            $(function(){
            	$("#cm").click(
            			function(){
            				$("input:checkbox[name='role[]']:checked").each(function () {
               			 		 $(this).prop("checked", false);
            				});
            			}
            	)
            	
			 })
             
            function ajaxDel(id){  
            	$.ajax({  
                    type:"GET",  
                    url:"{:url('admin/admin/del')}?id="+id,  
                    success:function(data){
                    	if(data=='no pri'){
                    		alert('你没有权限删除管理员');
                    	}else if(data==true){
                    		alert('删除成功');
                    		location.reload();
                    	}else{
                    		alert('删除失败');
                    	}
                    	
                      }

                });  
            }
            
          
            
             function ajaxEdit(id){  
            	$.ajax({  
                    type:"GET",  
                    url:"{:url('admin/admin/edit')}?id="+id,  
                    success:function(data){
                    		if(data=="no pri"){
                    			alert("你没有权限查看详情");
                    		}else if(data==false){
                    			alert("查询数据库失败");
                    		}else{
                    				$('#myModal').modal('show');
                    				$('#uid').val(data['id']);
		                    		$("#eu").val(data['username']);
		                    		$("#ep").val(data['password']);
		                    		if(data['email']==""){
		                    			$("#ee").val("无");
		                    		}else{
		                    			$("#ee").val(data['email']);
		                    		}
		                    		
		                    		if(data['is_use']){
		                    			$("#uon").attr('checked','true');
		                    		}else{
		                    			$("#uoff").attr('checked','true');
		                    		}
		                    		if(data.hasOwnProperty("role")){
		                    			for(i=0;i<data['role'].length;i++){
		                    				$("#rc"+data['role'][i]).prop("checked", true); 
		                    			}
		                    		}
	                    		}
                    	}

                });  
            }
            
             function ajaxPost(){  
                var formData = $("#edit").serialize();  
                
                $.ajax({  
                    type:"post",  
                    url:"{:url('admin/admin/doEdit')}",  
                    data:formData,
                    success:function(data){ 
                    	if(data=="no pri"){
                    		alert("你没有权限修改！");
                    	}else if(data==true){
                    		alert("提交成功");
                        	location.reload();
                    	}else{
                    		alert("提交失败！事务回滚");
                    	}
                        
                    },
                    

                });  
            }  
            
              
		</script> 
		
		
<!-- Start right content -->
<div class="content-page">
    <!-- ============================================================== -->
    <!-- Start Content here -->
    <!-- ============================================================== -->
    <div class="content">
        <!-- Page Heading Start -->
        <div class="page-heading">
            <h1><i class='fa fa-table'></i> 管理员模块</h1>
            </div>
        <!-- Page Heading End-->                <!-- Your awesome content goes here -->
        	<div class="col-md-2"></div>
        	
            <div class="col-md-8">
                <div class="widget">
                    <div class="widget-header transparent">
                        <h2><strong>管理员列表</strong> </h2>
                        <div class="additional-btn">
                            <a href="#" class="hidden reload"><i class="icon-ccw-1"></i></a>
                            <a href="#" class="widget-toggle"><i class="icon-down-open-2"></i></a>
                            <a href="#" class="widget-close"><i class="icon-cancel-3"></i></a>
                        </div>
                    </div>
                    <div class="widget-content">
                        <div class="table-responsive">
                            <table data-sortable class="table table-hover table-striped">
                                <thead>
                                <tr>
                                    <th>管理员ID</th>
                                    <th>管理员名称</th>
                                    <th>邮箱</th>
                                    <th>状态</th>
                                    <th style="text-align: center;">操作</th>
                                   
                                </tr>
                                </thead>

                                <tbody>
                                	{volist name="list" id="vo"}
                                <tr>
                                    <td>{$vo.id}</td>
                                    <td><strong>{$vo.username}</strong></td>
                                    <td><strong>{$vo.email}</strong></td>
                                   	<td><span class="label {$vo.is_use==1?'label-success':'label-default'}">{$vo.is_use==1?"启用":"禁用"}</span></td>
                                    <td>
                                    	<button class="btn btn-primary btn-sm" data-toggle="modal"  onclick="ajaxEdit({$vo.id})">编辑</button>
                                        <button type="button" class="btn btn-danger btn-sm" onclick="ajaxDel({$vo.id})">删除</button>
                                         
                                    </td>
                                   
                                </tr>
                               {/volist}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

            </div>
            
           
<!-- 用户模态框 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="text-align: center;">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" id="cm">&times;</button>
                <h4 class="modal-title" id="myModalLabel">编辑用户</h4>
            </div>
            <div class="modal-body clearfix">
            	 <form id="edit">
            	 	 <div class="col-md-1"> </div>
        	<table class="table-bordered col-md-10"  style="text-align: center;;border-collapse:separate;border-spacing:10px 10px;">
        		<trbody>
        			<tr>
        				<input type="hidden" name="id" id="uid" />
        			<td>
        				用户名
        				
        			</td>
        			<td>
        				<p style="margin-top:10px;">
        				<input type="text" name="username" id="eu"> 
        				</p>
        			</td>
        		</tr>
        		<tr>
        			<td>
        				
        				密码
        			</td>
        			<td>
        				<p style="margin-top:10px;">
        				<input type="password" name="password" id="ep">
        				</p>
        			</td>
        		</tr>
        		<tr>
        			<td>
        				
        				邮箱
        			</td>
        			<td>
        				<p style="margin-top:10px;">
        				<input type="email" name="email" id="ee">
        				</p>
        			</td>
        		</tr>
        		<tr>
        			<td >
        				是否启用?
        			</td>
        			<td>
        				<p style="margin-top: 10px;">
        				启用&nbsp;
        				<input type="radio" name="is_use" value="1" id="uon"/>
        				禁用&nbsp;
        				<input type="radio" name="is_use" value="0" id="uoff"/>
        				</p>
        			</td>
        			
        		</tr> 
        		<tr>
        			<td class="col-md-5">角色:</td>
        			<td >
        				{volist name="role" id="vo"}
        				<?php
        						if($i%2!=0){
        							echo "<p style='margin-top:10px;'>";
        							
        						}
        					?>
        				<span >{$vo.role_name}&nbsp;</span>
        				<span>
        					
        					<input type="checkbox" name="role[]" value="{$vo.id}" id="rc{$vo.id}"  />
        				</span>
        				<?php
        						
        						if($i%2==0){
        							echo "</p>";
        						}
        					?>
        				{/volist}
        			</td>
        			
        		</tr>
        		</trbody>
        	</table>
        	</form>
        	
        
            </div>
            <div class="modal-footer"  style="text-align: center;">
            	<button type="button" class="btn btn-primary" onclick="ajaxPost()">提交更改</button>
              </div>
            
        </div>
    </div>
</div>


        	
        
           
            <div class="modal-footer"  style="text-align: center;">
            	
            	<button type="button" class="btn btn-primary close" data-dismiss="modal" aria-hidden="true">关闭</button>
              </div>
            
        </div>
    </div>
</div>

   	<!-- Footer Start -->
        <footer class="col-md-12">
            Huban Creative &copy; 2014
            <div class="footer-links pull-right">
                <a href="#">About</a><a href="#">Support</a><a href="#">Terms of Service</a><a href="#">Legal</a><a
                    href="#">Help</a><a href="#">Contact Us</a>
            </div>
        </footer>
        <!-- Footer End -->
    </div>
    <!-- ============================================================== -->
    <!-- End content here -->
    <!-- ============================================================== -->

</div>

<!-- End right content -->

{/block}
